﻿//*** Guidance generated code ***//

using System;
using System.Linq;
using System.Web.Mvc;

using Castle.DynamicProxy;

using ProjectBase.Core;

namespace Survey.Tools.IoC
{
    public class FacadeDebugExceptionFilter : FacadeExceptionFilter
    {
        public override void Intercept(IInvocation invocation)
        {
            try
            {
                invocation.Proceed();
            }
            catch (Exception ex)
            {
                var target = invocation.InvocationTarget ?? invocation.Proxy;

                PublishingManager.Publish(this, ExceptionMessage.FormatWith(target, invocation.Method.Name), ex);

                if (target is IFacade) // common Exception handling for Facades, except DaoExceptions
                //&& !(ex is DaoException)) // for DEBUG even DaoException is rendered as a message
                {
                    PublishingManager.Publish(target, BusinessLayerError.FormatWith(target, invocation.Method.Name), ex);
                    invocation.ReturnValue = invocation.Method.ReturnParameter.DefaultValue;
                }
                else
                {
                    throw;
                }
            }
        }
    }
}